package com.qiantu.phone.ui.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.StringRes;

import com.hjq.base.BaseDialog;
import com.qiantu.phone.R;
import com.qiantu.phone.aop.SingleClick;
import com.qiantu.phone.app.AppApplication;

/**
 *    author : Android 轮子哥
 *    github : https://github.com/getActivity/AndroidProject
 *    time   : 2018/12/2
 *    desc   : 提示对话框
 */
public final class ConfirmHintDialog {

    public static final class Builder
            extends BaseDialog.Builder<Builder> {

        @Nullable
        private OnListener mListener;

        private final ImageView hintIcon;
        private final TextView tvHint;
        private final TextView tvContent;
        private final TextView btnLeft;
        private final TextView btnConfirm;
        private final View btnSpace;

        public Builder(Context context) {
            super(context);
            setContentView(R.layout.confirm_hint_dialog);
//            setCancelable(false);
//            setCanceledOnTouchOutside(false);
            setWidth(getResources().getDimensionPixelSize(R.dimen.dp_343));
            hintIcon = findViewById(R.id.icon_hint);
            tvHint = findViewById(R.id.tv_hint);
            tvContent = findViewById(R.id.tv_content);
            btnLeft = findViewById(R.id.btn_left);
            btnSpace = findViewById(R.id.btn_space);
            btnConfirm = findViewById(R.id.btn_confirm);
            setOnClickListener(btnLeft, btnConfirm);
        }

        public Builder setHintIcon(int res) {
            hintIcon.setImageDrawable(AppApplication.getInstance().getThemeDrawable(res));
            return this;
        }

        public Builder setHint(String text) {
            tvHint.setText(text);
            return this;
        }

        public Builder setContent(String text) {
            tvContent.setText(text);
            return this;
        }

        public Builder setContent(@StringRes int res) {
            tvContent.setText(res);
            return this;
        }

        public Builder setLeftBtnText(String text) {
            btnLeft.setText(text);
            if(TextUtils.isEmpty(text)){
                btnLeft.setVisibility(View.GONE);
                btnSpace.setVisibility(View.GONE);
            }else{
                btnLeft.setVisibility(View.VISIBLE);
                btnSpace.setVisibility(View.VISIBLE);
            }
            return this;
        }

        public Builder setRightBtnText(String text) {
            btnConfirm.setText(text);
            if(TextUtils.isEmpty(text)){
                btnConfirm.setVisibility(View.GONE);
            }else{
                btnConfirm.setVisibility(View.VISIBLE);
            }
            return this;
        }

        public Builder setLeftBtnStrongHint(boolean isStrong) {
            if(isStrong){
                btnLeft.setBackgroundResource(R.drawable.btn_theme_round_4dp);
                btnLeft.setTextColor(AppApplication.getInstance().getThemeColor(R.attr.textColor4));
                btnConfirm.setBackgroundResource(R.drawable.btn_secondary_color6_frame_4dp);
                btnConfirm.setTextColor(AppApplication.getInstance().getThemeColor(R.attr.secondaryColor6));
            }else{
                btnLeft.setBackgroundResource(R.drawable.btn_secondary_color6_frame_4dp);
                btnLeft.setTextColor(AppApplication.getInstance().getThemeColor(R.attr.secondaryColor6));
                btnConfirm.setBackgroundResource(R.drawable.btn_theme_round_4dp);
                btnConfirm.setTextColor(AppApplication.getInstance().getThemeColor(R.attr.textColor4));
            }
            return this;
        }

        public Builder setListener(OnListener listener) {
            mListener = listener;
            return this;
        }

        @Override
        public BaseDialog create() {
            return super.create();
        }

        @SingleClick
        @Override
        public void onClick(View view) {
            int viewId = view.getId();
            if (viewId == R.id.btn_confirm) {
                dismiss();
                if (mListener == null) {
                    return;
                }
                mListener.onConfirm(getDialog());
            }else if(viewId == R.id.btn_left){
                dismiss();
                if (mListener == null) {
                    return;
                }
                mListener.onLeftClick(getDialog());
            }
        }

        public Builder setRightBtnBackground(int resId) {
            btnConfirm.setBackgroundResource(resId);
            return this;
        }
    }

    public interface OnListener {

        void onLeftClick(BaseDialog dialog);
        void onConfirm(BaseDialog dialog);
    }


}